<template>
  <!-- ref 一般绑定在子组件里 -->
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import Bscroll from "@better-scroll/core";
import Pullup from "@better-scroll/pull-up";
//Pullup这个上拉加载跟多是一个插件要安装不然不会生效
Bscroll.use(Pullup);

export default {
  name: "scroll",
  props: {
    probeType: {
      type: Number,
      default: 1,
    },
    pullUpLoad: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      scroll: null,
    };
  },
  mounted() {
    //生命周期函数

    const options = {
      probeType: this.probeType,
      click: true,
      cancelable: true,
      pullUpLoad: this.pullUpLoad,
    };
    //如果ref绑定在组件中那么通过this.$refs.wrapper就能获取到一个组件对象
    this.scroll = new Bscroll(this.$refs.wrapper, options);
    //监听滚动
    this.scroll.on("scroll", (position) => {
      //通过在定义事件把position传出去 谁要用就监听这个事件
      this.$emit("scroll", position);
    });
    //滚动底部
    this.scroll.on("pullingUp", () => {
      this.$emit("pullingUp");
    });
    // console.log(this.scroll);
  },
  methods: {
    finishPullUp() {
      this.scroll.finishPullUp();
    },
    refresh() {
      this.scroll && this.scroll.refresh();
    },
    scrollTo(x, y, time) {
      this.scroll && this.scroll.scrollTo && this.scroll.scrollTo(x, y, time);
    },
  },
};
</script>

<style>
</style>